﻿Public Class Busqueda_Rapida

    Enum estado
        insertar
        modificar
    End Enum

    Enum termino
        aprobado
        rechazado
    End Enum

    Public tipo_busqueda As String
    Dim nombre As String

    Public Property _Nombre
        Get
            Return nombre
        End Get
        Set(ByVal value)
            nombre = value
        End Set
    End Property

    Dim acceso As New acceso_a_datos
    Dim primera_busqueda As Boolean = False

    Private Sub Busqueda_Rapida_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.txt_doc.Focus()
        btn_eliminar.Enabled = False
    End Sub

    'Detectar si se colocaron numeros en los campos correspondientes
    Private Sub txt_numDoc_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_doc.KeyPress
        Select Case Asc(e.KeyChar)
            Case 4, 24, 4, 19, 127, 13, 9, 15, 14, 8
                Exit Sub
        End Select

        If IsNumeric(e.KeyChar) = False Then
            MsgBox("Este carater no es un número ( " + e.KeyChar + " )", vbCritical, "Importante")
            e.KeyChar = ""
        End If
    End Sub

    'BOTON NUMERO
    Private Sub btn_doc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_doc.Click

        If Me.txt_doc.Text = "" Then
            MsgBox("Debe ingresar un dato para realizar la busqueda", vbExclamation, "Atencion")
            Me.txt_doc.Focus()
            Exit Sub
        End If

        If Me.txt_doc.Text <> "" Then
            Me.grilla_Empleados.Rows.Clear()
            Me.buscar_por_num_doc()
            Me.txt_ape.Enabled = False
            Me.txt_nom.Enabled = False
            lbl_ape_nom.Enabled = False
            Me.btn_ape.Enabled = False
            Me.btn_nombre.Enabled = False
            Me.btn_ape_nom.Enabled = False
            Me.flecha.Visible = True
            Me.lbl_nueva.Visible = True
            Me.lbl_extra.Visible = True
            btn_eliminar.Enabled = True
            Exit Sub
        End If
    End Sub

    'BUSCAR POR NUMERO
    Public Sub buscar_por_num_doc()
        Dim tabla As New Data.DataTable
        Dim consulta As String = ""
        Dim c As Integer

        tipo_busqueda = "numero"

        consulta = "SELECT H.fecha, H.horario_Ingreso, ISNULL (H.horario_Egreso, '00:00:00') as 'horario_Egreso'"
        consulta += "FROM Horario H INNER JOIN Empleado E ON H.numero_Doc = E.numero_Doc "
        consulta += "WHERE H.numero_Doc=" & Me.txt_doc.Text

        tabla = acceso._consulta(consulta)

        If tabla.Rows.Count <> 0 Then
            For c = 0 To tabla.Rows.Count - 1
                Me.grilla_Empleados.Rows.Add(FormatDateTime(tabla.Rows(c)("fecha"), DateFormat.ShortDate), FormatDateTime(tabla.Rows(c)("horario_Ingreso"), DateFormat.ShortTime), FormatDateTime(tabla.Rows(c)("horario_Egreso"), DateFormat.ShortTime))
            Next
            Exit Sub
        Else
            MsgBox("No se encontro ningun horario para esta persona", vbExclamation, "Importante")
            Me.grilla_Empleados.Rows.Clear()
            Me.txt_doc.Text = ""
            Me.txt_doc.Focus()
        End If
    End Sub

    'BOTON APELLIDO
    Private Sub btn_ape_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ape.Click
        If Me.txt_ape.Text = "" Then
            MsgBox("Debe ingresar un dato para realizar la busqueda", vbExclamation, "Atencion")
            Me.txt_ape.Focus()
            Exit Sub
        End If

        If Me.txt_ape.Text <> "" Then
            Me.grilla_Empleados.Rows.Clear()
            Me.buscar_por_ape()
            Me.txt_doc.Enabled = False
            Me.txt_nom.Enabled = False
            lbl_ape_nom.Enabled = False
            Me.btn_ape_nom.Enabled = False
            Me.btn_doc.Enabled = False
            Me.btn_nombre.Enabled = False
            Me.flecha.Visible = True
            Me.lbl_nueva.Visible = True
            Me.lbl_extra.Visible = True
            btn_eliminar.Enabled = True
            Exit Sub
        End If
    End Sub

    'BUSCAR POR APELLIDO
    Public Sub buscar_por_ape()
        Dim tabla As New Data.DataTable
        Dim consulta As String = ""
        Dim c As Integer

        tipo_busqueda = "apellido"

        consulta = "SELECT H.fecha, H.horario_Ingreso, ISNULL (H.horario_Egreso, '00:00:00') as 'horario_Egreso'"
        consulta += "FROM Horario H INNER JOIN Empleado E ON H.numero_Doc = E.numero_Doc "
        consulta += "WHERE E.apellido LIKE '" & Me.txt_ape.Text & "%' COLLATE SQL_LATIN1_GENERAL_CP1_CI_AI"

        tabla = acceso._consulta(consulta)

        If tabla.Rows.Count <> 0 Then
            For c = 0 To tabla.Rows.Count - 1
                Me.grilla_Empleados.Rows.Add(FormatDateTime(tabla.Rows(c)("fecha"), DateFormat.ShortDate), FormatDateTime(tabla.Rows(c)("horario_Ingreso"), DateFormat.ShortTime), FormatDateTime(tabla.Rows(c)("horario_Egreso"), DateFormat.ShortTime))
            Next
            Exit Sub
        Else
            MsgBox("No se encontro ningun horario para esta persona", vbExclamation, "Importante")
            Me.grilla_Empleados.Rows.Clear()
            Me.txt_doc.Text = ""
            Me.txt_doc.Focus()
        End If
    End Sub

    'BOTON NOMBRE
    Private Sub btn_nombre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nombre.Click
        If Me.txt_nom.Text = "" Then
            MsgBox("Debe ingresar un dato para realizar la busqueda", vbExclamation, "Atencion")
            Me.txt_nom.Focus()
            Exit Sub
        End If

        If Me.txt_nom.Text <> "" Then
            Me.grilla_Empleados.Rows.Clear()
            Me.buscar_por_nom()
            Me.txt_doc.Enabled = False
            Me.txt_ape.Enabled = False
            lbl_ape_nom.Enabled = False
            Me.btn_ape_nom.Enabled = False
            Me.btn_doc.Enabled = False
            Me.btn_ape.Enabled = False
            Me.flecha.Visible = True
            Me.lbl_nueva.Visible = True
            Me.lbl_extra.Visible = True
            btn_eliminar.Enabled = True
            Exit Sub
        End If
    End Sub

    'BUSCAR POR NOMBRE
    Public Sub buscar_por_nom()
        Dim tabla As New Data.DataTable
        Dim consulta As String = ""
        Dim c As Integer

        tipo_busqueda = "nombre"

        consulta = "SELECT H.fecha, H.horario_Ingreso, ISNULL (H.horario_Egreso, '00:00:00') as 'horario_Egreso'"
        consulta += "FROM Horario H INNER JOIN Empleado E ON H.numero_Doc=E.numero_Doc "
        consulta += "WHERE E.nombre LIKE '%" & Me.txt_nom.Text & "%' COLLATE SQL_LATIN1_GENERAL_CP1_CI_AI"

        tabla = acceso._consulta(consulta)

        If tabla.Rows.Count <> 0 Then
            For c = 0 To tabla.Rows.Count - 1
                Me.grilla_Empleados.Rows.Add(FormatDateTime(tabla.Rows(c)("fecha"), DateFormat.ShortDate), FormatDateTime(tabla.Rows(c)("horario_Ingreso"), DateFormat.ShortTime), FormatDateTime(tabla.Rows(c)("horario_Egreso"), DateFormat.ShortTime))
            Next
            Exit Sub
        Else
            MsgBox("No se encontro ningun horario para esta persona", vbExclamation, "Importante")
            Me.grilla_Empleados.Rows.Clear()
            Me.txt_doc.Text = ""
            Me.txt_doc.Focus()
        End If
    End Sub

    'BOTON APELLIDO Y NOMBRE
    Private Sub btn_ape_nom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ape_nom.Click
        If Me.txt_ape.Text = "" Or Me.txt_nom.Text = "" Then
            MsgBox("Debe ingresar un dato para realizar la busqueda", vbExclamation, "Atencion")
            Me.txt_ape.Focus()
            Exit Sub
        End If

        If Me.txt_nom.Text <> "" And Me.txt_nom.Text <> "" Then
            Me.grilla_Empleados.Rows.Clear()
            Me.buscar_por_ape_nom()
            Me.txt_doc.Enabled = False
            Me.btn_doc.Enabled = False
            Me.btn_ape.Enabled = False
            Me.btn_nombre.Enabled = False
            Me.flecha.Visible = True
            Me.lbl_nueva.Visible = True
            Me.lbl_extra.Visible = True
            btn_eliminar.Enabled = True
            Exit Sub
        End If
    End Sub

    'BUSCAR POR APELLIDO Y NOMBRE
    Public Sub buscar_por_ape_nom()
        Dim tabla As New Data.DataTable
        Dim consulta As String = ""
        Dim c As Integer

        tipo_busqueda = "ape_nom"

        consulta = "SELECT H.fecha, H.horario_Ingreso, ISNULL (H.horario_Egreso, '00:00:00') as 'horario_Egreso'"
        consulta += "FROM Horario H INNER JOIN Empleado E ON H.numero_Doc = E.numero_Doc "
        consulta += "WHERE E.apellido LIKE '" & Me.txt_ape.Text & "%' COLLATE SQL_LATIN1_GENERAL_CP1_CI_AI "
        consulta += "AND E.nombre LIKE '%" & Me.txt_nom.Text & "%' COLLATE SQL_LATIN1_GENERAL_CP1_CI_AI"

        tabla = acceso._consulta(consulta)

        If tabla.Rows.Count <> 0 Then
            For c = 0 To tabla.Rows.Count - 1
                Me.grilla_Empleados.Rows.Add(FormatDateTime(tabla.Rows(c)("fecha"), DateFormat.ShortDate), FormatDateTime(tabla.Rows(c)("horario_Ingreso"), DateFormat.ShortTime), FormatDateTime(tabla.Rows(c)("horario_Egreso"), DateFormat.ShortTime))
            Next
            Exit Sub
        Else
            MsgBox("No se encontro ningun horario para esta persona", vbExclamation, "Importante")
            Me.grilla_Empleados.Rows.Clear()
            Me.txt_ape.Text = ""
            Me.txt_nom.Text = ""
            Me.txt_ape.Focus()
        End If
    End Sub

    'NUEVA BUSQUEDA
    Private Sub lbl_nueva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl_nueva.Click
        Me.grilla_Empleados.Rows.Clear()
        Me.txt_doc.Text = ""
        Me.txt_ape.Text = ""
        Me.txt_nom.Text = ""
        Me.txt_doc.Enabled = True
        Me.txt_ape.Enabled = True
        Me.txt_nom.Enabled = True
        lbl_ape_nom.Enabled = True
        Me.btn_ape_nom.Enabled = True
        Me.btn_doc.Enabled = True
        Me.btn_ape.Enabled = True
        Me.btn_nombre.Enabled = True
        Me.flecha.Visible = False
        Me.lbl_nueva.Visible = False
        Me.lbl_extra.Visible = False
        btn_eliminar.Enabled = False
    End Sub

    'BOTON SALIR    
    Private Sub btn_salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_salir.Click
        Me.Close()
    End Sub

    'para buscar por numero doc con enter
    Private Sub txt_doc_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_doc.KeyPress
        If Asc(e.KeyChar) = 13 Then
            btn_doc_Click(sender, e)
        End If
    End Sub

    'para buscar por apellido con enter
    Private Sub txt_ape_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_ape.KeyPress
        If Asc(e.KeyChar) = 13 Then
            btn_ape_Click(sender, e)
        End If
    End Sub

    'para buscar por nombre con enter
    Private Sub txt_nom_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_nom.KeyPress
        If Asc(e.KeyChar) = 13 And Me.txt_ape.Text = "" Then
            btn_nombre_Click(sender, e)
        End If

        If Asc(e.KeyChar) = 13 And Me.txt_ape.Text <> "" Then
            btn_ape_nom_Click(sender, e)
        End If
    End Sub

    Private Sub btn_eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_eliminar.Click
        If verficar_horarios_anteriores() = termino.aprobado Then
            Seguridad_eliminar_reg._Nombre = nombre
            Seguridad_eliminar_reg.ShowDialog()
        Else
            MsgBox("No existen hoarios que eliminar para este usuario", vbInformation, "Importante")
        End If
    End Sub

    'FUNCION PARA verificar si hay horarios de meses anteriores al actual
    Private Function verficar_horarios_anteriores() As termino
        Dim comando As String = ""
        Dim consulta As String = ""
        Dim tabla As Data.DataTable

        consulta = "SELECT * FROM Horario H INNER JOIN Empleado E ON H.numero_Doc=E.numero_Doc "
        consulta += "WHERE Month(H.fecha)<" & Now.Month

        tabla = acceso._consulta(consulta)

        If tabla.Rows.Count > 0 Then
            Return termino.aprobado
            Exit Function
        Else
            Return termino.rechazado
            Exit Function
        End If
    End Function

    Private Sub Busqueda_Rapida_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Dispose()
    End Sub
End Class